/* eslint-disable no-unused-vars */
import React, { Component } from 'react';
import { View, NativeAppEventEmitter} from 'react-native';
import { Navigation } from 'react-native-navigation';
import { Provider } from 'react-redux';
import initSubscriber from 'redux-subscriber';
import SplashScreen from 'react-native-splash-screen';

import { registerScreens } from './screens';

// import { iconsMap, iconsLoaded } from './utils/AppIcons';
import configureStore from './store/configureStore';

import {global} from './modules/actions';

const store = configureStore();
const subscribe = initSubscriber(store);

registerScreens(store, Provider);

const navigatorStyle = {
	navBarTextColor: 'white',
	navBarButtonColor: 'white',
	navBarBackgroundColor: '#EA5404',
	statusBarTextColorScheme: 'light',
	// drawUnderTabBar: true,
};

class App extends Component {
	constructor(props) {
		super(props);
	    // store.subscribe(this._onStoreUpdate.bind(this));
		this.unsession = subscribe('session', state => {
		    let { session } = state;
			if(session && session.user){
				session.reload && this.startApp('main');
			}else{
				this.startApp('login');
			}
		});

		this.unwelcome = subscribe('global.welcomedone', state => {
		    let { global } = state;
			if(!global.welcomedone){
				this.startApp('welcome');
			}
		});

		store.dispatch(global.global_init(this));
		// 欢迎页面结束后，调用下面的语句，目前还没做
		store.dispatch(global.global_welcome_done());

		// After having done stuff (such as async tasks) hide the splash screen
		SplashScreen.hide();
	}

	componentDidMount(){

	}

	// _onStoreUpdate(){
	// 	let { global, session } = store.getState();
	//
	// 	if (!global.initialized){// app初始化
	//         // this.startApp('not-init');
	//     }else{
	//         if (!global.welcomedone) {
	//             this.startApp('welcome');
	//         } else {
	// 			if(session && session.user){//登录过的
	// 				session.reload && this.startApp('main');
	// 			}else{//没登录过或者失效
	// 				this.startApp('login');
	// 			}
	//         }
	//     }
	// }

	componentWillUnmount(){
		this.unsession();
		this.unwelcome();

		this.subscription && this.subscription.remove();
	}

	startApp(root) {
		switch (root) {
	      case 'welcome':

	        break;
	       case 'login': //登录界面
	            Navigation.startSingleScreenApp({
	              screen: {
	                  screen: 'housekeeperapp.loginindex',
	                  animated: true,
	                  navigatorStyle: {
	                      navBarHidden:true,
						  statusBarTextColorScheme: 'light'
	                  }
	              },
	              passProps: {
	              },
	              animationType: 'fade'
	            });
	            break;
	      case 'main':
			  Navigation.startTabBasedApp({
				  tabs: [
					  {
						  label: '消息',
						  screen: 'housekeeperapp.message',
						  icon: require('./img/hk_nav_msg.png'),
						  selectedIcon: require('./img/hk_nav_msg_active.png'),
						  title: '管家来了',
						  navigatorStyle
					  },
					  {
						  label: '订单',
						  screen: 'housekeeperapp.order',
						  icon: require('./img/hk_nav_order.png'),
						  selectedIcon: require('./img/hk_nav_order_active.png'),
						  title: '我的订单',
						  navigatorStyle,
						  navigatorButtons:{
  			                rightButtons:[
  			                    {
									title: '新增任务',
  			                        id: 'add',
  			                    }
  			                ]
  			            }
					  },
					  {
						  label: '账户',
						  screen: 'housekeeperapp.account',
						  icon: require('./img/hk_nav_account.png'),
						  selectedIcon: require('./img/hk_nav_account_active.png'),
						  title: '我的账户',
						  navigatorStyle:{
							  ...navigatorStyle,
							  navBarHidden: true
						  }
					  }
				  ],
				  tabsStyle: {
					  tabBarButtonColor: '#B5B5B5',
					  tabBarSelectedButtonColor: '#EA5404',
					  tabBarBackgroundColor: 'white'
				  },
				  animationType: 'fade'
			  });
	        break;
	      default:
	        console.error('Unknown app root');
	    }
	}
}

export default App;
